package com.atguigu.dao.impl;

import com.atguigu.dao.BeanDao;
import com.atguigu.dao.BookDao;
import com.atguigu.pojo.Book;

import java.util.List;

/**
 * @Author luoyin
 * @Date 17:18 2022/7/18
 * @Description 描述
 **/
public class BookDaoImpl extends BeanDao<Book> implements BookDao {
    @Override
    public List<Book> selectAll() throws Exception {
        return selectList(
                Book.class,
                "select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath from t_book"
        );
    }

    @Override
    public Long selectTotalSize()throws Exception {
        return selectCount("select count(*) from t_book");
    }

    @Override
    public List<Book> selectLimit(int begin, int pageSize) throws Exception {
        return selectList(
                Book.class,
                "select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath from t_book limit ?,?",
                begin,
                pageSize
        );
    }

    @Override
    public int add(Book book) throws Exception {
        return update(
                "insert into t_book values(null,?,?,?,?,?,?)",
                book.getBookName(),
                book.getAuthor(),
                book.getPrice(),
                book.getSales(),
                book.getStock(),
                book.getImgPath()
        );
    }

    @Override
    public int update(Book book) throws Exception {
        return update(
                "update t_book set book_name=?,author = ?,price=?,sales=?,stock=?,img_path=? where book_id = ?",
                book.getBookName(),
                book.getAuthor(),
                book.getPrice(),
                book.getSales(),
                book.getStock(),
                book.getImgPath(),
                book.getBookId()
        );
    }

    @Override
    public Book selectById(int bookId) throws Exception {
        return selectOne(
                Book.class,
                "select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath from t_book where book_id = ?",
                bookId
        );
    }

    @Override
    public int delete(int bookId) throws Exception {
        return update(
                "delete from t_book where book_id = ?",
                bookId
        );
    }

    @Override
    public Long selectListTotalSize(Double floorPrice, Double peakPrice) throws Exception {
        return selectCount(
                "select count(*) from t_book where price >= ? and price <= ?",
                floorPrice,
                peakPrice
        );
    }


    @Override
    public List<Book> selectList(Double floorPrice, Double peakPrice, int begin, int pageSize) throws Exception {
        return selectList(
                Book.class,
                "select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath from t_book where price >= ? and price <= ? limit ?,?",
                floorPrice,
                peakPrice,
                begin,
                pageSize
        );
    }

    @Override
    public void updateBatch(Object[][] bookParamArr) throws Exception {
        batch(
                "update t_book set sales = sales + ? ,stock = stock - ? where book_id = ?",
                bookParamArr
        );
    }
}
